﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp5.Grades
{
    public partial class ShowGrade : Form
    {
        public ShowGrade()
        {
            InitializeComponent();
        }

        private void ShowGrade_Load(object sender, EventArgs e)
        {
            dataGridView1.DataSource = WJNDBEntities.dbhelp.Grade.ToList();
            comboBox1.Text = "请选择查询类型";
            comboBox2.Enabled = false;
            comboBox3.Enabled = false;
            comboBox2.DataSource = WJNDBEntities.dbhelp.Student.ToList();
            comboBox2.DisplayMember = "SNo";
            comboBox3.DataSource = WJNDBEntities.dbhelp.Lesson.ToList();
            comboBox3.DisplayMember = "LNo";
        }

        private void dataGridView1_RowPrePaint(object sender, DataGridViewRowPrePaintEventArgs e)
        {
            
            //if (e.RowIndex < gradeBindingSource.Count)
            //{
            //    comboBox1.Text = "in";
            //    var prod = (Grade)gradeBindingSource[e.RowIndex];

            //    var grid = dataGridView1;
            //    if (prod.Lesson != null)
            //    {
            //        grid.Rows[e.RowIndex].Cells[Lname.Index].Value = prod.Lesson.LName;
            //    }

            //    if (prod.Student != null)
            //    {
            //        grid.Rows[e.RowIndex].Cells[sname.Index].Value = prod.Student.SName;
            //    }
            //}
            var dgv = dataGridView1;
            
            string id = Convert.ToString(dgv.Rows[e.RowIndex].Cells[sNoDataGridViewTextBoxColumn.Index].Value).Trim();
            string lno = Convert.ToString(dgv.Rows[e.RowIndex].Cells[lNoDataGridViewTextBoxColumn.Index].Value).Trim();
            Student s = WJNDBEntities.dbhelp.Student.Where(o => o.SNo == id).FirstOrDefault();
            Lesson l = WJNDBEntities.dbhelp.Lesson.Where(o => o.LNo == lno).FirstOrDefault();
            if (s != null && l != null)
            {
                dgv.Rows[e.RowIndex].Cells[lessonDataGridViewTextBoxColumn.Index].Value = l.LName;
                dgv.Rows[e.RowIndex].Cells[studentDataGridViewTextBoxColumn.Index].Value = s.SName;
            }

        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (comboBox1.SelectedIndex == 0)
            {
                comboBox2.Enabled = true;
                comboBox3.Enabled = false;
            }
            else if(comboBox1.SelectedIndex == 1)
            {
                comboBox2.Enabled = false;
                comboBox3.Enabled = true;
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
           
            if (comboBox1.SelectedIndex == 0)
            {
                string id = comboBox2.Text.Trim();
                dataGridView1.DataSource = WJNDBEntities.dbhelp.Grade.Where(o => o.SNo == id).ToList();
            }
            else if (comboBox1.SelectedIndex == 1)
            {
                string lno = comboBox3.Text.Trim();
                dataGridView1.DataSource = WJNDBEntities.dbhelp.Grade.Where(o => o.LNo == lno).ToList();
            }
           
        }

        private void dataGridView1_DataSourceChanged(object sender, EventArgs e)
        {
            textBox1.Text = dataGridView1.RowCount+"";
            if (dataGridView1.RowCount < 1)
            {
                MessageBox.Show("查询为空！");
            }
        }
    }
}
